<?php
require_once 'getList.php';
$unmark_array = getList();
?>
<html>
<head lang="zh-cn">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Java课程批阅小助手</title>

    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="//cdn.bootcss.com/summernote/0.8.2/summernote.css" rel="stylesheet">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="container text-center">
    <h1>Java课程批阅小助手</h1>
    <div>
        <ol class="breadcrumb">
            <li><a href="/java-course-helper">实验列表</a></li>
            <li class="active">批阅列表</li>
        </ol>
    </div>
    <div class="row">
        <a href="/java-course-helper" class="btn btn-primary " role="button">返回实验列表</a>
        <button class="btn btn-default" disabled="disabled">下载所有未批阅作业</button>
    </div>
    <div class="row">
        <!-- 显示所有的实验-->
        <table class="table">
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>提交时间</th>
                <th>作业</th>
                <th>分数</th>
            </tr>
            <?php
            foreach ($unmark_array as $unmark)
            {
                echo '<tr id="'.$unmark['mark_link'].'">';

                echo '<td id="student_id">'.$unmark['student_id'].'</td>';
                echo '<td id="name">'.$unmark['name'].'</td>';
                echo '<td id="commit_time">'.$unmark['commit_time'].'</td>';
                echo '<td>'.'<a id="download-link" target="_blank" href=getassignmentfile.php?_r=0&hwaid='.$unmark['mark_link'].'>下载</a>'.'</td>';
                if ($unmark['score'] == "未批阅") {
                    echo '<td><button id="mark-button" value="'.$unmark['mark_link'].'" class="btn btn-warning" data-backdrop="static" data-toggle="modal" data-target="#myModal">'.$unmark['score'].'</button></td>';
                } else {
                    echo '<td><button id="mark-button" value="'.$unmark['mark_link'].'" class="btn btn-success" data-backdrop="static" data-toggle="modal" data-target="#myModal">'.$unmark['score'].'</button></td>';
                }
                echo '</tr>';
            }
            ?>
        </table>
        <ul id="pagination-demo" class="pagination-sm"></ul>

    </div>


</div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="markModalTitle">xxx</h4>
            </div>
            <form id="form" role="form">
            <div class="modal-body">
                <input type="hidden" name="hwaid" value="">
                <input type="hidden" name="stuid" value="">
                <input type="hidden" name="stuname" value="">
                <input type="hidden" name="labname" value="<?=$_GET['name']?>">
                <input type="hidden" name="jumpseq" value="0">
                <input type="hidden" name="fullmark" value="30">
                <div id="summernote"></div>
                <textarea id="summernote" name="IPT_BODY" style="display: none"></textarea>
                <div class="form-group">
                    <label for="comments" class="sr-only">评语</label>
                    <textarea class="form-control" rows="3" name="comments" placeholder="评语"></textarea>
                </div>
                <div class="form-group row">
                    <div class="col-md-2">
                        <label for="mark" class="sr-only">分数</label>
                        <input type="number" class="form-control" name="mark" placeholder="分数">
                    </div>
                    <div class="checkbox col-md-offset-6 col-md-4">
                        <label>
                            <input name="sendnotification" value="mail" checked="checked" type="checkbox"> 邮件发送批阅信息
                        </label>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <span id="loading" style="display: none"></span>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="commit-button" class="btn btn-primary">提交</button>
            </div>
            </form>
        </div>
    </div>
</div>

<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="//cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="//cdn.bootcss.com/twbs-pagination/1.4.1/jquery.twbsPagination.min.js"></script>
<script src="//cdn.bootcss.com/summernote/0.8.2/summernote.min.js"></script>
<script src="//cdn.bootcss.com/summernote/0.8.2/lang/summernote-zh-CN.js"></script>
<script>

    $(document).ready(function() {
        $('#summernote').summernote({
            height: 300,                 // set editor height
            minHeight: null,             // set minimum height of editor
            maxHeight: null,             // set maximum height of editor
            focus: true,                  // set focus to editable area after initializing summernote
            lang: 'zh-CN'
        });
    });

    $(document).on({
        ajaxSend: function() { $('#commit-button').prop("disabled",true); $('#loading').text('正在处理中~');$('#loading').fadeIn("3000");}
    });

    var hwaid  = 0;
    $('#myModal').on('show.bs.modal', function (e) {
        // 清空数据
        $('#summernote').summernote('code', '');
        $('textarea[name=comments]').val('');

        // 获取批阅的学生参数

        var button = e.relatedTarget;
        hwaid = button.value;
        $('#markModalTitle').text($('#'+button.value+' '+'#name').text()+'的批阅');
        $('input[name=stuid]').val($('#'+button.value+' '+'#student_id').text());
        $('input[name=hwaid]').val(button.value);
        $('input[name=stuname]').val($('#'+button.value+' '+'#name').text());

        // 已批阅显示批阅结果
        if ($(button).html() != "未批阅") {
            var markcontent_link = $('#'+button.value+' '+'#download-link').attr('href').replace('_r=0','_r=1');
            $.getJSON(markcontent_link,function (result) {
                $('#commit-button').prop("disabled",false);
                $('#loading').text('读取数据成功！');
                $('#loading').delay("slow").fadeOut("3000");
                $('#summernote').summernote('code', result.ipt_body);
                $('textarea[name=comments]').val(result.comments);
                $('input[name=mark]').attr('value',result.mark);
            })
        };
    })
    
    
    $('#commit-button').click(function () {
        $('textarea[name=IPT_BODY]').val($('#summernote').summernote('code'));
        //console.log( $('#summernote').serialize() );
        console.log($('#form').serialize());
        $.post("commit_mark.php",$('#form').serialize(),function (data) {
            if (data = 200)
            {
                $('#commit-button').prop("disabled",false);
                $('#loading').text('提交成功！');
                $('#loading').delay("slow").fadeOut("3000");
            }

        });
    })

    // 关闭提交窗口后，刷新页面
    $('#myModal').on('hidden.bs.modal',function (e) {
        window.location.reload();
    })

    // 分页功能
//    $('#pagination-demo').twbsPagination({
//        totalPages: 35,
//        visiblePages: 3,
//        first: '首页',
//        prev: '上一页',
//        next: '下一页',
//        last: '末页',
//        onPageClick: function (event, page) {
//            $('#page-content').text('Page ' + page);
//        }
//    });

</script>
</body>
</html>
